﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Xml.Linq;

namespace NoteBox
{
    
    
    public partial class MailForm : Form
    {
        // МАССИВ ПОЧТЫ
        public static List<mMail> mails = new List<mMail>();
        public static List<mButton> mailButtons = new List<mButton>();
        //
        public static int activeMail = 0;
        //
        public MailForm()
        {
            InitializeComponent();
            this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
        }

        // Профили
        public static bool ReadMailProfilesUserFromFile()
        {
            if (!File.Exists(Form1.TasksFileName))
                return false;
            int cnt = 0;
            XDocument XDoc = XDocument.Load(Form1.TasksFileName);
            foreach (XElement elem in XDoc.Element("Elements").Element("UserMailProfiles").Elements())
            {
                mails.Add(new mMail(
                    (string)elem.Element("Email"),
                    (string)elem.Element("Login"),
                    (string)elem.Element("Password"),
                    (string)elem.Element("Server"),
                    (int)elem.Element("Port"),
                    (bool)elem.Element("Ssl"),
                    (bool)elem.Element("AutoCheck"),
                    (int)elem.Element("AutoCheckTime")
                ));
                mails[mails.Count - 1].Parent = Form1.formMail;
                //
                int Lleft =12;
                if (mailButtons.Count - 1 >= 0) Lleft = mailButtons[mailButtons.Count - 1].Left + mailButtons[mailButtons.Count - 1].Width;
                mailButtons.Add(new mButton(mails[mails.Count - 1].mMailName, mails.Count - 1, 11, Lleft));
                //
                // Теперь загружаем сообщения из соответствующего ящичка.
                //LoadMessageFromFile(mails.Count - 1, Application.UserAppDataPath + "\\" + mails[mails.Count - 1].mMailName);
                mails[mails.Count - 1].LoadMessageFromFile("");
                //
                cnt++;
            }
            try
            {
                mails[activeMail].Visible = true;
                foreach (mButton mb in mailButtons)
                    mb.SetPassive();
                mailButtons[activeMail].SetActive();
            }
            catch { }
            return true;
        }
        
        #region moveForm //////////////   Обработка перемещений формы
        bool formMoved = false;
        bool formResizedH = false;
        int XPos = 0;
        int YPos = 0;

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            XPos = e.X;
            YPos = e.Y;
            if (e.Y > Height - 7)
            {
                formResizedH = true;
            }
            else
                formMoved = true;
            this.BringToFront();
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            int maxleft = Screen.PrimaryScreen.WorkingArea.Width - Width;
            int maxtop = Screen.PrimaryScreen.WorkingArea.Height - Height;
            int maxh0 = Screen.PrimaryScreen.WorkingArea.Top;
            int maxl0 = Screen.PrimaryScreen.WorkingArea.Left;
            if (formMoved || formResizedH)
            {
                formMoved = false;
                formResizedH = false;
                Cursor = Cursors.Default;
                if (Left > maxleft)
                    Left = maxleft;
                if (Left < maxl0)
                    Left = maxl0;
                if (Top > maxtop)
                    Top = maxtop;
                if (Top < maxh0)
                    Top = maxh0;
                for (int i=0; i<mails.Count; i++)
                    mails[i].HeightSizeChange();
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Y > Height - 7)
                Cursor = Cursors.HSplit;
            else
                Cursor = Cursors.Default;
            if (formMoved)
            {
                Top += e.Y - this.YPos;
                Left += e.X - this.XPos;
            }
            if (formResizedH)
            {
                Height = e.Y;
                mails[activeMail].HeightSizeChange();
            }
        }
        #endregion


        

        private void buttonSettings_Click(object sender, EventArgs e)
        {
            Settings FormSettings = new Settings(2);
            FormSettings.Show();
            Form1.TaskChanged = true;
        }

        private void buttonReceive_Click(object sender, EventArgs e)
        {
            foreach (mMail ml in mails)
            {
                Thread Th = new Thread(ml.ReceiveMail);
                Th.Start();
            }
        }

        private void buttonhide_Click(object sender, EventArgs e)
        {
            Visible = false;
        }

               
        
    }
}
